package HomeWork2;

public class BankTest {

    Object obj=new Object();
    int money=1000;

    public void Ahuman()  {
        while(money>0) {
            synchronized (obj){
                Thread.currentThread().setName("A用户");
                money = money - 100;
                if(money>0) {
                    System.out.println(Thread.currentThread().getName() + "取走" + 100 + "元" + "剩余钱数：" + money + "元");
                }else{
                   System.exit(0);
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    public void Bhuman(){
        while(money>0){
            synchronized (obj){
                Thread.currentThread().setName("B用户");
                money=money-100;
                if (money>0) {
                    System.out.println(Thread.currentThread().getName() + "取走" + 100 + "元" + "剩余钱数：" + money + "元");
                }else{
                    System.exit(0);
                }
                }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    public void Chuman() {
        while (true) {
            synchronized (obj) {
                Thread.currentThread().setName("C用户");
                money=money+2000;
                System.out.println(Thread.currentThread().getName() + "存入" + 2000+ "元"+"剩余钱数：" + money + "元");
                //obj.notify();
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        BankTest bankTest=new BankTest();
        new Thread(()->bankTest.Ahuman()).start();
        new Thread(()->bankTest.Bhuman()).start();
        new Thread(()->bankTest.Chuman()).start();
    }
}